package com.dindang.maintenance.mvp.ui.adapter.multitype;

import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dindang.maintenance.R;
import com.dindang.maintenance.app.utils.TextTransformUtils;
import com.dindang.maintenance.mvp.model.CommontCons;
import com.dindang.maintenance.mvp.model.entity.Maintain;
import com.dindang.maintenance.mvp.model.entity.Repair;
import com.luck.picture.lib.tools.StringUtils;

import java.util.List;

/**
 * Created by cxw on & 2018/12/26
 */
public class MaintainViewAdapter extends RecyclerView.Adapter<MaintainViewAdapter.ViewHolder> {

    List<Repair.RepairList.RowsBean> list;
    OnClickListener listener;

    public MaintainViewAdapter(List<Repair.RepairList.RowsBean> list, OnClickListener listener) {
        this.list = list;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.maintenance_item_maintain, parent, false);
        MaintainViewAdapter.ViewHolder viewHolder = new MaintainViewAdapter.ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.cl_maintenace.setOnClickListener(v -> listener.onClick(position));
        String area = "";
        if(list.get(position).getC_applyArea().equals("1")){
            area="私有区域";
        }else {
            area="公共区域";
        }
        holder.tvArea.setText(area);
        holder.tvNum.setText(list.get(position).getC_numCard());
//        状态(1:待派单2:待接单3:待完成4:暂停中5:待回访6:已回访9已撤销)
        String state = "";
        if(list.get(position).getC_status().equals("1")){
            state="待派单";
        }else if(list.get(position).getC_status().equals("2")){
            state="待接单";
        }else if(list.get(position).getC_status().equals("3")){
            state="待完成";
        }else if(list.get(position).getC_status().equals("4")){
            state="暂停中";
        }else if(list.get(position).getC_status().equals("5")){
            state="待回访";
        }else if(list.get(position).getC_status().equals("6")){
            state="已回访";
        }else if(list.get(position).getC_status().equals("9")){
            state="已撤销";
        }else {
            state="未知状态";
        }
        holder.tvState.setText(state);
        if(list.get(position).getC_applyName()!=null){
            holder.tvRepairMan.setText(list.get(position).getC_applyName());
        }
        if(list.get(position).getC_errtype()!=null){
            if(TextTransformUtils.string2Int(list.get(position).getC_errtype())<5&&TextTransformUtils.string2Int(list.get(position).getC_subtype())<6){
                if(list.get(position).getC_errtype().equals("1")){
                    holder.tvType.setText(CommontCons.err_type[TextTransformUtils.string2Int(list.get(position).getC_errtype())]+"-"+CommontCons.sub_type1[TextTransformUtils.string2Int(list.get(position).getC_subtype())]);
                }else if(list.get(position).getC_errtype().equals("2")){
                    holder.tvType.setText(CommontCons.err_type[TextTransformUtils.string2Int(list.get(position).getC_errtype())]+"-"+CommontCons.sub_type2[TextTransformUtils.string2Int(list.get(position).getC_subtype())]);
                }else if(list.get(position).getC_errtype().equals("3")){
                    holder.tvType.setText(CommontCons.err_type[TextTransformUtils.string2Int(list.get(position).getC_errtype())]+"-"+CommontCons.sub_type3[TextTransformUtils.string2Int(list.get(position).getC_subtype())]);
                }else if(list.get(position).getC_errtype().equals("4")){
                    holder.tvType.setText(CommontCons.err_type[TextTransformUtils.string2Int(list.get(position).getC_errtype())]+"-"+CommontCons.sub_type4[TextTransformUtils.string2Int(list.get(position).getC_subtype())]);
                }else {
                    holder.tvType.setText(CommontCons.err_type[TextTransformUtils.string2Int(list.get(position).getC_errtype())]+"-"+CommontCons.sub_type4[TextTransformUtils.string2Int(list.get(position).getC_subtype())]);
                }
            }
        }
        if(list.get(position).getC_address()!=null){
            holder.tvAddress.setText(list.get(position).getC_address());
        }
        if(list.get(position).getD_createdate()!=null){
            holder.tvTime.setText(list.get(position).getD_createdate());
        }
        if(list.get(position).getC_name()!=null){
            holder.tvWhere.setText(list.get(position).getC_name());
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ConstraintLayout cl_maintenace;
        TextView tvArea;
        TextView tvNum;
        TextView tvState;
        TextView tvRepairMan;
        TextView tvType;
        TextView tvAddress;
        TextView tvTime;
        TextView tvWhere;
        ViewHolder(View itemView) {
            super(itemView);
            cl_maintenace = itemView.findViewById(R.id.maintenance_cl_maintenace);
            tvArea  = itemView.findViewById(R.id.maintenance_tv_maintenance_area);
            tvNum = itemView.findViewById(R.id.maintenance_tv_maintenance_num);
            tvState = itemView.findViewById(R.id.maintenance_tv_state);
            tvRepairMan= itemView.findViewById(R.id.maintenance_tv_repair_man);
            tvType = itemView.findViewById(R.id.maintenance_tv_team_man);
            tvAddress = itemView.findViewById(R.id.maintenance_tv_fault_amaintenanceress);
            tvTime = itemView.findViewById(R.id.maintenance_tv_time);
            tvWhere = itemView.findViewById(R.id.maintenance_tv_address);
        }
    }

    public interface OnClickListener{
        void onClick(int position);
    }

}
